package cn.guangboyuan.dynamindatasource.controller;


import cn.guangboyuan.dynamindatasource.entity.SysUser;
import cn.guangboyuan.dynamindatasource.model.RespResult;
import cn.guangboyuan.dynamindatasource.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户 前端控制器
 *
 * @author RedAnts
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping
    public RespResult<Boolean> insertSysUser(@RequestBody SysUser sysUser) {
        RespResult<Boolean> respResult;
        try {
            sysUserService.insertSysUser(sysUser);
            respResult = RespResult.ok(true);
        } catch (Exception e) {
            respResult = RespResult.error(e.getMessage());
        }
        return respResult;
    }

    @GetMapping("/{userId}")
    public RespResult<SysUser> findUserById(@PathVariable("userId") Long userId) {
        RespResult<SysUser> respResult;
        try {
            SysUser sysUser = sysUserService.findUserById(userId);
            respResult = RespResult.ok(sysUser);
        } catch (Exception e) {
            respResult = RespResult.error(e.getMessage());
        }
        return respResult;
    }
}
